{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tkinter - 计算器\n",
    "- 模拟系统的计算器功能\n",
    "- 实现一个简单的具有加减法等操作的计算器\n",
    "- 使用tkinter\n",
    "- 操作步骤\n",
    "    - 画GUI\n",
    "    - 给每个控件配置相应的事件\n",
    "    - 写逻辑代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 第一步： 画出图形界面上部\n",
    "from tkinter import *\n",
    "\n",
    "\n",
    "root = Tk()\n",
    "# 定义面板的大小\n",
    "root.geometry('250x380')\n",
    "root.title(\"北京图灵学院\")\n",
    "\n",
    "\n",
    "\n",
    "# 定义面板\n",
    "# bg代表背景颜色（background）， #dddddd是十六进制表示颜色的一个串\n",
    "frame_show = Frame(width=300, height=150, bg='#dddddd')\n",
    "frame_show.pack()\n",
    "\n",
    "# 定义顶部区域\n",
    "\n",
    "sv = StringVar()\n",
    "sv.set('0')\n",
    "\n",
    "# anchor:定义控件的锚点，e代表右边\n",
    "# font代表字体\n",
    "show_label = Label(frame_show, textvariable=sv, \\\n",
    "                  bg='green', width=12, height=1,\\\n",
    "                  font=(\"黑体\", 20, 'bold'),\\\n",
    "                  justify=LEFT, anchor='e')\n",
    "show_label.pack(padx=10, pady=10)\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+\n"
     ]
    }
   ],
   "source": [
    "# 第二步： 画出图形界面下半部\n",
    "from tkinter import *\n",
    "\n",
    "\n",
    "root = Tk()\n",
    "# 定义面板的大小\n",
    "root.geometry('250x380')\n",
    "root.title(\"北京图灵学院\")\n",
    "\n",
    "\n",
    "\n",
    "# 定义面板\n",
    "# bg代表背景颜色（background）， #dddddd是十六进制表示颜色的一个串\n",
    "frame_show = Frame(width=300, height=150, bg='#dddddd')\n",
    "frame_show.pack()\n",
    "\n",
    "# 定义顶部区域\n",
    "\n",
    "sv = StringVar()\n",
    "sv.set('0')\n",
    "\n",
    "# anchor:定义控件的锚点，e代表右边\n",
    "# font代表字体\n",
    "show_label = Label(frame_show, textvariable=sv, \\\n",
    "                  bg='green', width=12, height=1,\\\n",
    "                  font=(\"黑体\", 20, 'bold'),\\\n",
    "                  justify=LEFT, anchor='e')\n",
    "show_label.pack(padx=10, pady=10)\n",
    "\n",
    "def delete():# 第二步： 画出图形界面下半部\n",
    "from tkinter import *\n",
    "\n",
    "\n",
    "root = Tk()\n",
    "# 定义面板的大小\n",
    "root.geometry('250x380')\n",
    "root.title(\"北京图灵学院\")\n",
    "\n",
    "\n",
    "\n",
    "# 定义面板\n",
    "# bg代表背景颜色（background）， #dddddd是十六进制表示颜色的一个串\n",
    "frame_show = Frame(width=300, height=150, bg='#dddddd')\n",
    "frame_show.pack()\n",
    "\n",
    "# 定义顶部区域\n",
    "\n",
    "sv = StringVar()\n",
    "sv.set('0')\n",
    "\n",
    "# anchor:定义控件的锚点，e代表右边\n",
    "# font代表字体\n",
    "show_label = Label(frame_show, textvariable=sv, \\\n",
    "                  bg='green', width=12, height=1,\\\n",
    "                  font=(\"黑体\", 20, 'bold'),\\\n",
    "                  justify=LEFT, anchor='e')\n",
    "show_label.pack(padx=10, pady=10)\n",
    "\n",
    "def delete():\n",
    "    print(\"我被删除了\")\n",
    "def fan():\n",
    "    print(\"烦了\")\n",
    "    \n",
    "def clear():\n",
    "    print(\"科利尔\")\n",
    "\n",
    "# 按键区域\n",
    "frame_bord = Frame(width=400, height=350, bg='#cccccc')\n",
    "\n",
    "b_del = Button(frame_bord, text=\"←\", width=5, height=1, \\\n",
    "              command=delete)\n",
    "b_del.grid(row=0, column=0)\n",
    "\n",
    "button_clear = Button(frame_bord,text = 'C',width = 5,height =1,\\\n",
    "                      command = clear).grid(row = 0,column = 1)\n",
    "button_fan = Button(frame_bord,text = '±',width = 5,height =1,\\\n",
    "                    command = fan).grid(row = 0,column = 2)\n",
    "button_ce = Button(frame_bord,text = 'CE',width = 5,height =1,\\\n",
    "                   command = clear).grid(row = 0,column = 3)\n",
    "\n",
    "def change(num):\n",
    "    print(num)\n",
    "    \n",
    "def operation(op):\n",
    "    print(op)\n",
    "    \n",
    "    \n",
    "b_1 = Button(frame_bord, text='1', width=5, height=2,\\\n",
    "            command=lambda:change(\"1\"))\n",
    "b_1.grid(row=1, column=0)\n",
    "\n",
    "\n",
    "b_jia = Button(frame_bord, text='+', width=5, height=2,\\\n",
    "            command=lambda:operation(\"+\"))\n",
    "b_jia.grid(row=2, column=0)\n",
    "\n",
    " \n",
    "\n",
    "frame_bord.pack(padx=10, pady=10)\n",
    "\n",
    "root.mainloop()\n",
    "    print(\"我被删除了\")\n",
    "def fan():\n",
    "    print(\"烦了\")\n",
    "    \n",
    "def clear():\n",
    "    print(\"科利尔\")\n",
    "\n",
    "# 按键区域\n",
    "frame_bord = Frame(width=400, height=350, bg='#cccccc')\n",
    "\n",
    "b_del = Button(frame_bord, text=\"←\", width=5, height=1, \\\n",
    "              command=delete)\n",
    "b_del.grid(row=0, column=0)\n",
    "\n",
    "button_clear = Button(frame_bord,text = 'C',width = 5,height =1,\\\n",
    "                      command = clear).grid(row = 0,column = 1)\n",
    "button_fan = Button(frame_bord,text = '±',width = 5,height =1,\\\n",
    "                    command = fan).grid(row = 0,column = 2)\n",
    "button_ce = Button(frame_bord,text = 'CE',width = 5,height =1,\\\n",
    "                   command = clear).grid(row = 0,column = 3)\n",
    "\n",
    "def change(num):\n",
    "    print(num)\n",
    "    \n",
    "def operation(op):\n",
    "    print(op)\n",
    "    \n",
    "    \n",
    "b_1 = Button(frame_bord, text='1', width=5, height=2,\\\n",
    "            command=lambda:change(\"1\"))\n",
    "b_1.grid(row=1, column=0)\n",
    "\n",
    "\n",
    "b_jia = Button(frame_bord, text='+', width=5, height=2,\\\n",
    "            command=lambda:operation(\"+\"))\n",
    "b_jia.grid(row=2, column=0)\n",
    "\n",
    " \n",
    "\n",
    "frame_bord.pack(padx=10, pady=10)\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#添加逻辑功能\n",
    "# 第二步： 画出图形界面下半部\n",
    "from tkinter import *\n",
    "\n",
    "\n",
    "root = Tk()\n",
    "# 定义面板的大小\n",
    "root.geometry('250x380')\n",
    "root.title(\"北京图灵学院\")\n",
    "\n",
    "\n",
    "\n",
    "# 定义面板\n",
    "# bg代表背景颜色（background）， #dddddd是十六进制表示颜色的一个串\n",
    "frame_show = Frame(width=300, height=150, bg='#dddddd')\n",
    "frame_show.pack()\n",
    "\n",
    "# 定义顶部区域\n",
    "\n",
    "sv = StringVar()\n",
    "sv.set('0')\n",
    "\n",
    "# anchor:定义控件的锚点，e代表右边\n",
    "# font代表字体\n",
    "show_label = Label(frame_show, textvariable=sv, \\\n",
    "                  bg='green', width=12, height=1,\\\n",
    "                  font=(\"黑体\", 20, 'bold'),\\\n",
    "                  justify=LEFT, anchor='e')\n",
    "show_label.pack(padx=10, pady=10)\n",
    "\n",
    "def delete():\n",
    "    print(\"我被删除了\")\n",
    "def fan():\n",
    "    print(\"烦了\")\n",
    "    \n",
    "def clear():\n",
    "    print(\"科利尔\")\n",
    "\n",
    "# 按键区域\n",
    "frame_bord = Frame(width=400, height=350, bg='#cccccc')\n",
    "\n",
    "b_del = Button(frame_bord, text=\"←\", width=5, height=1, \\\n",
    "              command=delete)\n",
    "b_del.grid(row=0, column=0)\n",
    "\n",
    "button_clear = Button(frame_bord,text = 'C',width = 5,height =1,\\\n",
    "                      command = clear).grid(row = 0,column = 1)\n",
    "button_fan = Button(frame_bord,text = '±',width = 5,height =1,\\\n",
    "                    command = fan).grid(row = 0,column = 2)\n",
    "button_ce = Button(frame_bord,text = 'CE',width = 5,height =1,\\\n",
    "                   command = clear).grid(row = 0,column = 3)\n",
    "'''\n",
    "考虑以下集中情况：\n",
    "1. 按下数字\n",
    "2. 按下操作符号\n",
    "3，只考虑两个操作数操作，比考虑复杂情况\n",
    "\n",
    "'''\n",
    "num1 = ''\n",
    "num2 = ''\n",
    "operator = ''\n",
    "\n",
    "def change(num):\n",
    "    '''\n",
    "    按下一个数字需要考虑两种情况：\n",
    "    1. 数字属于第一个操作数\n",
    "    2. 数字属于第二个操作数\n",
    "    3. 判断是否属于第一个操作数，可以通过operator判断\n",
    "    '''\n",
    "    # 加入操作数是None，表明肯定是第一个操作数\n",
    "    if not operator:\n",
    "        num1 = num1 + num\n",
    "        # 如果是第一个操作数，则只显示第一个操作数\n",
    "        sv.set(num1)\n",
    "    else:\n",
    "        num2 = num2 + num\n",
    "        # 如果是第二个操作数 ，则应该显示完整的计算式子\n",
    "        sv.set(num1+operator+num2)\n",
    "        \n",
    "    \n",
    "def operation(op):\n",
    "    if op in ['+', '-', 'x', '/']:\n",
    "        operator = op\n",
    "    else: # 认为是按下的等于号\n",
    "        if op == \"+\":\n",
    "            rst = int(num1) + int(num2)\n",
    "        if op == \"-\":\n",
    "            rst = int(num1) + int(num2)\n",
    "        if op == \"x\":\n",
    "            rst = int(num1) + int(num2)\n",
    "        if op == \"/\":\n",
    "            rst = int(num1) + int(num2)\n",
    "            \n",
    "        sv.set(str(rst))\n",
    "        \n",
    "        \n",
    "b_1 = Button(frame_bord, text='1', width=5, height=2,\\\n",
    "            command=lambda:change(\"1\"))\n",
    "b_1.grid(row=1, column=0)\n",
    "\n",
    "\n",
    "b_jia = Button(frame_bord, text='+', width=5, height=2,\\\n",
    "            command=lambda:operation(\"+\"))\n",
    "b_jia.grid(row=2, column=0)\n",
    "\n",
    " \n",
    "\n",
    "frame_bord.pack(padx=10, pady=10)\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
